﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace org.ids_adi.iring.adapter
{
  using org.ids_adi.iring.adapter.dtoLayer;
  using org.ids_adi.iring.adapter.dtoLayer.Model;
  using org.ids_adi.iring.adapter.eSUB;
  using org.ids_adi.iring.adapter.dtoLayer.Model.eSUB;
  using org.ids_adi.iring.adapter.dtoLayer.eSUB;
  using Microsoft.Practices.Unity;
  using Microsoft.Practices.Unity.Configuration;
  using System.Configuration;
  using org.ids_adi.iring.utility;

  public class DataService : IDataService
  {
    private DataServiceProvider _dataServiceProvider = null;



    /// <summary>
    /// Adapter Service Constructor
    /// </summary>
    public DataService()
    {
      _dataServiceProvider = new DataServiceProvider();
    }

    public org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves eSUBGetValvesREST(string identifier)
    {
      return _dataServiceProvider.eSUBGetValvesREST(identifier);
    }

    public Dictionary<string, string> eSUBGetValvesListREST()
    {
      return _dataServiceProvider.eSUBGetValvesListREST();
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesResponse eSUBGetValves(Identifier identifier)
    {
      return _dataServiceProvider.eSUBGetValves(identifier);
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesListResponse eSUBGetValvesList()
    {
      return _dataServiceProvider.eSUBGetValvesList();
    }  

  }

  public partial class DataServiceProvider : IDataService
  {
    IUnityContainer _container = null;
    private IDTOLayer _dtoFactory = null;
    private ConfigSettings _configSettings = null;


    #region CONSTANTS
    private const string CONFIGURATION_FILENAME = "Configuration.xml";
    #endregion

    public DataServiceProvider()
    {
      _configSettings = new ConfigSettings();
      string _profilePath = @".\App_Code" + @"\" + "eSUB" + @"\";
      _configSettings = Utility.Read<ConfigSettings>(_profilePath + CONFIGURATION_FILENAME, true);

      _configSettings.BaseDirectoryPath = System.AppDomain.CurrentDomain.BaseDirectory;
      _configSettings.TripleStoreConnectionString = System.Configuration.ConfigurationManager.AppSettings["TripleStoreConnectionString"];
      _configSettings.ModelDTOPath = System.Configuration.ConfigurationManager.AppSettings["ModelDTOPath"];
      _configSettings.IdsPath = System.Configuration.ConfigurationManager.AppSettings["IDataServicePath"];
      _configSettings.InterfaceServer = System.Configuration.ConfigurationManager.AppSettings["InterfaceService"];
      _configSettings.TrimData = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["TrimData"]);
      _configSettings.UseSemweb = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["UseSemweb"]);
      _configSettings.UseEDMX = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["UseEDMX"]);
      _configSettings.EncryptedToken = System.Configuration.ConfigurationManager.AppSettings["TargetCredentialToken"];
      _configSettings.EncryptedProxyToken = System.Configuration.ConfigurationManager.AppSettings["ProxyCredentialToken"];
      _configSettings.ProxyHost = System.Configuration.ConfigurationManager.AppSettings["ProxyHost"];
      _configSettings.ProxyPort = System.Configuration.ConfigurationManager.AppSettings["ProxyPort"];
      _configSettings.TransformPath = System.Configuration.ConfigurationManager.AppSettings["TransformPath"];
      _configSettings.CSDLPath = System.Configuration.ConfigurationManager.AppSettings["CSDLPath"];
      _configSettings.SSDLPath = System.Configuration.ConfigurationManager.AppSettings["SSDLPath"];
      _configSettings.MSLPath = System.Configuration.ConfigurationManager.AppSettings["MSLPath"];
      _configSettings.EDMXPath = System.Configuration.ConfigurationManager.AppSettings["EDMXPath"];
      _configSettings.EntityConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[_configSettings.EntitiesName].ConnectionString;

      try
      {
        _container = new UnityContainer();
        UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        section.Containers.Default.Configure(_container);
        _container.RegisterInstance<ConfigSettings>("ConfigSettings", _configSettings);
        ConfigSettings configSettings = _container.Resolve<ConfigSettings>("ConfigSettings");
        _dtoFactory = _container.Resolve<IDTOLayer>(configSettings.DtoLayerName);
      }
      catch (ResolutionFailedException ex)
      {
        _container = null;
        throw ex;
      }
      catch (Exception ex)
      {
        throw ex;
      }

    }

    public org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves eSUBGetValvesREST(string identifier)
    {
      try
      {
        return (org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves)_dtoFactory.GetDTO("Valves", identifier);
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO with identifier " + identifier + ". " + exception.ToString(), exception);
      }
    }

    public Dictionary<string, string> eSUBGetValvesListREST()
    {
      try
      {
        return _dtoFactory.GetDTOList("Valves");
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting ValvesDTO list. " + exception.ToString(), exception);
      }
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesResponse eSUBGetValves(Identifier identifier)
    {
      try
      {
        org.ids_adi.iring.adapter.eSUB.ValvesResponse response = new org.ids_adi.iring.adapter.eSUB.ValvesResponse();
        response.dto = (org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves)_dtoFactory.GetDTO("Valves", identifier.value);

        return response;
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO with identifier " + identifier.value + ". " + exception.ToString(), exception);
      }
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesListResponse eSUBGetValvesList()
    {
      try
      {
        List<DataTransferObject> commonDTOList = (List<DataTransferObject>)(_dtoFactory.GetList("Valves"));

        List<org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves> ValvesList = new List<org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves>();
        foreach (DataTransferObject dto in commonDTOList)
        {
          ValvesList.Add((org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves)dto);
        }

        org.ids_adi.iring.adapter.eSUB.ValvesListResponse response = new org.ids_adi.iring.adapter.eSUB.ValvesListResponse();
        response.dtoList = ValvesList;

        return response;
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO list. " + exception.ToString(), exception);
      }
    }

    public org.ids_adi.iring.adapter.dtoLayer.Model.TW.Valves TWGetValvesREST(string identifier)
    {
      try
      {
        return (org.ids_adi.iring.adapter.dtoLayer.Model.TW.Valves)_dtoFactory.GetDTO("Valves", identifier);
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO with identifier " + identifier + ". " + exception.ToString(), exception);
      }
    }

    public Dictionary<string, string> TWGetValvesListREST()
    {
      try
      {
        return _dtoFactory.GetDTOList("Valves");
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting ValvesDTO list. " + exception.ToString(), exception);
      }
    }

    public org.ids_adi.iring.adapter.TW.ValvesResponse TWGetValves(Identifier identifier)
    {
      try
      {
        org.ids_adi.iring.adapter.TW.ValvesResponse response = new org.ids_adi.iring.adapter.TW.ValvesResponse();
        response.dto = (org.ids_adi.iring.adapter.dtoLayer.Model.TW.Valves)_dtoFactory.GetDTO("Valves", identifier.value);

        return response;
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO with identifier " + identifier.value + ". " + exception.ToString(), exception);
      }
    }

    public org.ids_adi.iring.adapter.TW.ValvesListResponse TWGetValvesList()
    {
      try
      {
        List<DataTransferObject> commonDTOList = (List<DataTransferObject>)(_dtoFactory.GetList("Valves"));

        List<org.ids_adi.iring.adapter.dtoLayer.Model.TW.Valves> ValvesList = new List<org.ids_adi.iring.adapter.dtoLayer.Model.TW.Valves>();
        foreach (DataTransferObject dto in commonDTOList)
        {
          ValvesList.Add((org.ids_adi.iring.adapter.dtoLayer.Model.TW.Valves)dto);
        }

        org.ids_adi.iring.adapter.TW.ValvesListResponse response = new org.ids_adi.iring.adapter.TW.ValvesListResponse();
        response.dtoList = ValvesList;

        return response;
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO list. " + exception.ToString(), exception);
      }
    }
   
  }
}